import { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { EDB_SET_TREE_SELECTED } from './constants';

export function setTreeSelected(selected) {
  return {
    type: EDB_SET_TREE_SELECTED,
    selected,
  };
}

export function useSetTreeSelected() {
  const selectedIndicator = useSelector(store => store.edb.selectedIndicator);
  const dispatch = useDispatch();
  const boundAction = useCallback((...params) => dispatch(setTreeSelected(...params)), [dispatch]);
  return { setTreeSelected: boundAction, selectedIndicator };
}

export function reducer(state, action) {
  switch (action.type) {
    case EDB_SET_TREE_SELECTED:
      return {
        ...state,
        selectedIndicator: [...state.selectedIndicator, action.selected],
      };

    default:
      return state;
  }
}
